package com.hopechart.micro.util;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * <br/>文件工具类
 * </p>
 *
 * @author daijiang@hopechart.com
 * @version V1.0.0
 * @date 2018/2/5 14:41
 * @copyright 杭州鸿泉物联网技术股份有限公司
 */
public final class FileUtil {

    public static File getFile(String path, boolean create) {
        try {
            File file = new File(path);
            if (file.exists()) {
                return file;
            } else {
                if (create) {
                    file.createNewFile();
                } else {
                    throw new RuntimeException("文件不存在");
                }
            }
            return file;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("文件构建失败", e);
        }

    }

    public static File getFile(String path) {
        return getFile(path, false);
    }

    public static List<String> readFileLines(String readPath) {
        try {
            String[] lines = jodd.io.FileUtil.readLines(getFile(readPath));
            return Arrays.asList(lines);
        } catch (IOException e) {
            throw new RuntimeException("数据文件读取异常", e);
        }
    }

    public static List<String> readFileLines(File file) {
        try {
            String[] lines = jodd.io.FileUtil.readLines(file);
            return Arrays.asList(lines);
        } catch (IOException e) {
            throw new RuntimeException("数据文件读取异常", e);
        }
    }
}